﻿using AutoMapper;
using IOA.MES.Common;
using IOA.MES.DataAccess.Entity;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace IOA.MES.Business
{
    /// <summary>
    /// 流水线管理（错误代码：132001）
    /// </summary>
    public class AssemblyLineMgr
    {
        private MESContext mesDb = new MESContext("MES");

        /// <summary>
        /// 获取流水线对象
        /// </summary>
        /// <param name="lineId">流水线ID</param>
        /// <returns>流水线对象</returns>
        public VAssemblyLine GetLine(int lineId)
        {
            var entity = mesDb.AssemblyLines.AsNoTracking().FirstOrDefault(_ => _.PkId == lineId && _.Valid);
            return Mapper.Map<VAssemblyLine>(entity);
        }

        /// <summary>
        /// 编辑流水线
        /// </summary>
        /// <param name="model">流水线对象</param>
        public void EditLine(VAssemblyLine model)
        {
            var workshop = new WorkshopMgr().GetWorkshop(model.WorkshopCode);
            if (workshop == null)
            {
                throw new MultiLanBizException(132001);// "车间不存在");
            }
            model.WorkshopName = workshop.WorkshopName;
            if (model.PkId <= 0)
            {
                var exist = mesDb.AssemblyLines.FirstOrDefault(_ => _.WorkshopCode == model.WorkshopCode && _.LineName == model.LineName && _.Valid);
                if (exist != null)
                {
                    throw new MultiLanBizException(132002);// "流水线名称已存在，请更换其他名称");
                }
                model.Status = CommonStatus.Normal;
                model.CreateTime = DateTime.Now;
                model.ModifyBy = 0;
                var entity = Mapper.Map<AssemblyLine>(model);
                mesDb.AssemblyLines.Add(entity);
            }
            else
            {
                var exist = mesDb.AssemblyLines.FirstOrDefault(_ => _.WorkshopCode == model.WorkshopCode && _.LineName == model.LineName && _.PkId != model.PkId && _.Valid);
                if (exist != null)
                {
                    throw new MultiLanBizException(132002);// "流水线名称已存在，请更换其他名称");
                }
                var entity = mesDb.AssemblyLines.FirstOrDefault(_ => _.PkId == model.PkId && _.Valid);
                if (entity == null)
                {
                    throw new MultiLanBizException(132003);// "流水线不存在或已被删除，无法编辑");
                }
                entity.LineName = model.LineName;
                entity.WorkshopCode = model.WorkshopCode;
                entity.WorkshopName = model.WorkshopName;
                entity.ModifyBy = model.ModifyBy;
                entity.ModifyTime = DateTime.Now;
            }
            mesDb.SaveChanges();
        }

        /// <summary>
        /// 查询流水线列表
        /// </summary>
        /// <param name="pageIndex">页面索引</param>
        /// <param name="pageSize">页面容量</param>
        /// <param name="total">总数</param>
        /// <param name="workshopCode">车间ID</param>
        /// <param name="status">状态</param>
        /// <returns>流水线列表</returns>
        public List<VAssemblyLine> QueryLines(int pageIndex, int pageSize, out int total, string workshopCode, CommonStatus? status)
        {
            var query = mesDb.AssemblyLines.AsNoTracking().Where(_ => _.Valid);
            if (status.HasValue)
            {
                query = query.Where(_ => _.Status == status.Value);
            }
            if (!string.IsNullOrWhiteSpace(workshopCode))
            {
                query = query.Where(_ => _.WorkshopCode == workshopCode);
            }

            total = query.Count();
            query = query.OrderBy(_ => _.WorkshopCode).ThenBy(_ => _.LineName);

            var entities = pageSize <= 0 ? query.ToList() : query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var models = Mapper.Map<List<VAssemblyLine>>(entities);
            return models;
        }

        /// <summary>
        /// 查询流水线列表
        /// </summary>
        /// <param name="workshopCode">车间ID</param>
        /// <returns>流水线列表</returns>
        public List<VAssemblyLine> QueryLines(string workshopCode)
        {
            var entities = mesDb.AssemblyLines.Where(_ => _.WorkshopCode == workshopCode && _.Valid).ToList();
            return Mapper.Map<List<VAssemblyLine>>(entities);
        }

        /// <summary>
        /// 修改流水线状态
        /// </summary>
        /// <param name="model">流水线对象</param>
        public void ModifyStatus(VAssemblyLine model)
        {
            var exist = mesDb.AssemblyLines.FirstOrDefault(_ => _.PkId == model.PkId && _.Valid);
            if (exist == null)
            {
                throw new MultiLanBizException(132004);// "流水线不存在，无法修改状态");
            }
            exist.Status = model.Status;
            exist.ModifyBy = model.ModifyBy;
            exist.ModifyTime = DateTime.Now;
            mesDb.SaveChanges();
        }
    }
}
